<div class="sectioncontent">
    {{- $contentType := "all"}}
    {{- if eq (.Get "type") "section" }}
        {{- $contentType = "section" }}
    {{- end}}
    {{- $depthLimit := 2 }}
    {{- with .Get "depth" }}
        {{- $depthLimit = . }}
    {{- end }}

    {{- $startSection := .Page.Page }}
    {{- template "sectioncontent-loop" dict "startSection" $startSection "currentSection" $startSection "contentType" $contentType "depthLimit" $depthLimit "depthCnt" 0 }}
</div>

{{- define "sectioncontent-loop" }}
    {{- $startSection := .startSection }}
    {{- $currentSection := .currentSection }}
    {{- $contentType := .contentType }}
    {{- $depthLimit := .depthLimit }}
    {{- $depthCnt := add .depthCnt 1 }}
    {{- with .startSection }}
        <li>
            {{- with $currentSection }}
                {{- if and (ne $startSection $currentSection) (or (ne $contentType "section") .IsSection) }}
                        {{- if .LinkTitle }}
                            <a href="{{.RelPermalink}}">{{- .LinkTitle }}</a>
                        {{- else }}
                            {{- replace .File.Dir .Parent.File.Dir "" | replaceRE "/" "-" | replaceRE "-$" ":" }}
                            <a href="{{.RelPermalink}}">{{- .File.TranslationBaseName }}</a>
                        {{- end }}
                {{- end }}
            {{- end }}
            {{- if le $depthCnt $depthLimit }}
                <ul>
                    {{- $elem := dict }}
                    {{- range .Site.Pages }}
                        {{- if and (eq .CurrentSection $currentSection.CurrentSection) (ne $currentSection .) (ne $startSection .) (.IsDescendant $currentSection) }}
                            {{- $elem = merge $elem (dict (string .) .) }}
                        {{- else if and (eq $currentSection .Parent) (.IsSection) (ne .CurrentSection $currentSection.CurrentSection) (ne $startSection .) (.IsDescendant $currentSection) }}
                            {{- $elem = merge $elem (dict (string .) .) }}
                        {{- end }}
                    {{- end }}
                    {{- range $elem }}
                        {{- template "sectioncontent-loop" dict "startSection" $startSection "currentSection" . "contentType" $contentType "depthLimit" $depthLimit "depthCnt" $depthCnt }}
                    {{- end }}
                </ul>
            {{- end }}
        </li>
    {{- end }}
{{- end }}
